package com.thx.privilege.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thx.common.datasource.DataSource;
import com.thx.common.util.AppContext;
import com.thx.privilege.PriviManager;
import com.thx.privilege.PriviObj;
import com.thx.privilege.dao.PrivilegeRoleDao;
import com.thx.privilege.dao.PrivilegeUserDao;
import com.thx.privilege.model.PriviObjImpl;
import com.thx.privilege.model.PrivilegeRole;
import com.thx.privilege.model.PrivilegeUser;

/**
 * 建立日期 : 2014年5月7日  下午4:48:42<br>
 * 作者 : shiyan<br>
 * 模块 : 权限 <br>
 * 描述 : 权限管理service<br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 */
@Service
@DataSource("dataSourceB")
@Transactional(readOnly = true)
public class PriviManagerImpl implements PriviManager {
	@Autowired
	private PrivilegeRoleDao privilegeRoleDao;

	@Autowired
	private PrivilegeUserDao privilegeUserDao;
	
	@Override
	@Transactional(readOnly = false)
	public void appendPriviToUser(String userId, String objId, String priviType) {
		appendPriviToUser(userId,new String[]{objId},priviType);
	}

	@Override
	@Transactional(readOnly = false)
	public void appendPriviToUser(String userId, String[] objId,String priviType) {
		for(String objID : objId){
			if(!hasUserPrivi(userId,objID,priviType)){
				PrivilegeUser pu = new PrivilegeUser();
				pu.setUserId(userId);
				pu.setObjId(objID);
				pu.setType(priviType);
				String comId =AppContext.getCurrentComId();
				pu.setComId(comId);
				privilegeUserDao.save(pu);
			}
		}
	}

	@Override
	@Transactional(readOnly = false)
	public void appendPriviToRole(String roleId, String objId, String priviType) {
		appendPriviToRole(roleId,new String[]{objId},priviType);
	}

	@Override
	@Transactional(readOnly = false)
	public void appendPriviToRole(String roleId, String[] objId,String priviType) {
		for(String objID : objId){
			if(!hasRolePrivi(roleId,objID,priviType)){
				PrivilegeRole pr = new PrivilegeRole();
				pr.setRoleId(roleId);
				pr.setObjId(objID);
				pr.setType(priviType);
				String comId =AppContext.getCurrentComId();
				pr.setComId(comId);
				privilegeRoleDao.save(pr);
			}
		}
	}

	@Override
	@Transactional(readOnly = false)
	public void reAuthToUser(String userId, String objId, String priviType) {
		reAuthToUser(userId,new String []{objId},priviType);
	}

	@Override
	@Transactional(readOnly = false)
	public void reAuthToUser(String userId, String[] objId, String priviType) {
		List<PrivilegeUser> pus =privilegeUserDao.getPrivileges(userId,priviType);
		List<PrivilegeUser> delpus = new ArrayList<PrivilegeUser>();
		List<String> objIds =new ArrayList<String>(Arrays.asList(objId));
		if(pus!=null && pus.size()>0){
			for(PrivilegeUser pu : pus){
				if(!objIds.contains(pu.getObjId())){
					delpus.add(pu);
				}else{
					objIds.remove(pu.getObjId());
				}
			}
		}
		for(PrivilegeUser pu : delpus){
			privilegeUserDao.delete(pu);
		}
		for(String objID: objIds){
			PrivilegeUser pu = new PrivilegeUser();
			pu.setUserId(userId);
			pu.setObjId(objID);
			pu.setType(priviType);
			String comId =AppContext.getCurrentComId();
			pu.setComId(comId);
			privilegeUserDao.save(pu);
		}
	}

	@Override
	@Transactional(readOnly = false)
	public void reAuthToRole(String roleId, String objId, String priviType) {
		reAuthToRole(roleId,new String []{objId},priviType);
	}

	@Override
	@Transactional(readOnly = false)
	public void reAuthToRole(String roleId, String[] objId, String priviType) {
		List<PrivilegeRole> prs =privilegeRoleDao.getPrivileges(roleId,priviType);
		List<PrivilegeRole> delprs = new ArrayList<PrivilegeRole>();
		List<String> objIds =new ArrayList<String>(Arrays.asList(objId));
		if(prs!=null && prs.size()>0){
			for(PrivilegeRole pr : prs){
				if(!objIds.contains(pr.getObjId())){
					delprs.add(pr);
				}else{
					objIds.remove(pr.getObjId());
				}
			}
		}
		for(PrivilegeRole pr : delprs){
			privilegeRoleDao.delete(pr);
		}
		for(String objID: objIds){
			PrivilegeRole pr = new PrivilegeRole();
			pr.setRoleId(roleId);
			pr.setObjId(objID);
			pr.setType(priviType);
			String comId =AppContext.getCurrentComId();
			pr.setComId(comId);
			privilegeRoleDao.save(pr);
		}
	}
	
	@Override
	public List<PriviObj> getUserPrivileges(String userId, String priviType) {
		List<PrivilegeUser> pus =privilegeUserDao.getPrivileges(userId,priviType);
		List<PriviObj> pos = new ArrayList<PriviObj>();
		for(PrivilegeUser pu : pus){
			PriviObjImpl po = new PriviObjImpl();
			po.setId(pu.getObjId());
			po.setType(pu.getType());
			pos.add(po);
		}
		return pos;
	}

	@Override
	public List<PriviObj> getRolePrivileges(String roleId, String priviType) {
		List<PrivilegeRole> prs =privilegeRoleDao.getPrivileges(roleId,priviType);
		List<PriviObj> pos = new ArrayList<PriviObj>();
		for(PrivilegeRole pr : prs){
			PriviObjImpl po = new PriviObjImpl();
			po.setId(pr.getObjId());
			po.setType(pr.getType());
			pos.add(po);
		}
		return pos;
	}

	@Override
	public boolean hasUserPrivi(String userId, String objId, String priviType) {
		PrivilegeUser pu =privilegeUserDao.getPrivileges(userId,objId,priviType);
		if(pu==null)
			return false;
		return true;
	}

	@Override
	public boolean hasRolePrivi(String roleId, String objId, String priviType) {
		PrivilegeRole pr =privilegeRoleDao.getPrivileges(roleId,objId,priviType);
		if(pr==null)
			return false;
		return true;
	}

	@Override
	@Transactional(readOnly = false)
	public void delPriUser(String objId,String type) {
		 privilegeUserDao.delPriUser(objId,type);
	}

	@Override
	@Transactional(readOnly = false)
	public void delPriRole(String objId,String type) {
		privilegeRoleDao.delPriRole(objId,type);
	}

}
